#pragma once

///顺序栈类型定义
template<typename E, int M>
struct SqStack
{
    E elem[M];
    int top;
};

///顺序栈的基本操作

///初始化栈为空 InitStack(&S)
template <typename E,int M>
void InitStack(SqStack<E,M> &S)
{
    S.top=0;
}
///判断栈是否为空 StackEmpty(S)
template <typename E,int M>
bool StackEmpty(const SqStack<E,M>&S)
{
    return S.top ==0;
}
///入栈 Push(&S, e)
template <typename E, int M>
void Push(SqStack<E,M>&S,E e)
{
    ///若栈满则抛出异常
    if(S.top==M) throw" stack full";
    ///否则，入栈
    S.elem[S.top++]=e;
}

///出栈 POp(&S,&e)
template<typename E,int M>
void Pop(SqStack<E,M> &S, E &e)
{
    ///若栈空，则抛出异常
    if (S.top ==0) throw "Stack empty";
    ///否则，出栈
    e = S.elem[--S.top];
}

/////出栈 Pop(&S) 返回栈顶元素
/// template<typename E, int M>
//E Pop(SqStack<E,M> &S)
///{
    ///若栈空，则抛出异常
    ///if(S.top ==0) throw "Stack empty";
    ///否则，出栈
    /// return S.elem[--s.top];
///}
///出栈 Pop(&S)
template<typename E,int M>
void Pop(SqStack<E,M> &S)
{
    ///若栈空，则抛出异常
    if (S.top ==0) throw "Stack empty";
    //否则，出栈
    --S.top;
}

///取栈顶元素  GetTop(S,&e)
template<typename E,int M>
void GetTop(const SqStack<E,M> &S, E &e)
{
    //若栈空，则抛出异常
    if (S.top == 0) throw "Stack empty";
    //否则，返回栈顶元素
    e = S.elem[S.top - 1];
}
///返回栈顶元素 GetTop(S)
template<typename E, int M>
E GetTop(const SqStack<E,M> &S)
{
    ///若栈空，则抛出异常
    if(S.top == 0) throw "Stack empty";
    ///否则，返回栈顶元素
    return S.elem[S.top - 1];
}